import javax.microedition.lcdui.*;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Random;
import javax.microedition.media.*;
import javax.microedition.lcdui.game.*;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;
import javax.microedition.rms.RecordStore;
import java.util.Stack;
import javax.microedition.rms.*;
import java.io.*;
public class cGame extends Canvas implements Runnable, Constants
{
	#include "defines.h"
	
#if SONY_K800i
	static final int SCREEN_W = 240;
	static final int SCREEN_H = 320;	
#else
	static final int SCREEN_W = SCR_W;
	static final int SCREEN_H = SCR_H;	
#endif
	
	Random rand = new Random();	
	
	boolean isInterrupted = false;
	public static int s_iCurrentState = GameState.GameloftLogo;
	public static Graphics s_g;
	public static boolean hasSound=true, hasSfx=true;
	static long m_lBeginTime, m_lCurrentTime;
	public static int s_frameCount = 0;
	public cGame()
	{
		//s_iCurrentState = GameState.MainMenu;
		s_iCurrentState=GameState.GameloftLogo;
		//setFullScreenMode(true);
		// #if UseBackBuffer
		//m_imgBackBuffer = Image.createImage(SCR_W, SCR_H);
		//m_gBackBuffer = m_imgBackBuffer.getGraphics();
		//SwitchState(GameState.MainMenu);
		new Thread(this).start();
	}
	
	protected void hideNotify()
	{
		isInterrupted = true;
		// gamePause();
		// saveRS(false);
	}
	
	protected void showNotify()
	{
	// gameResume();
	}
	
	
	public void UpdateGame(int mode)
	{		
		switch (s_iCurrentState)
		{
			case GameState.GameloftLogo:
				UpdateLogo(mode);
				break;
			case GameState.InGame:
				UpdateIngame(mode);
				break;
			case GameState.InGameMenu:
				UpdateIGMenu(mode);
				break;	
			case GameState.Splash:
				UpdateSplash(mode);
				break;
			case GameState.Loading:
				UpdateLoading(mode);
				break;
			case GameState.AskForSounds:
				UpdateSound(mode);
				break;
			case GameState.MainMenu:
				UpdateMainMenu(mode);
				break;
			case GameState.Exit:
				UpdateExit(mode);
				break;
			case GameState.About:
				UpdateAbout(mode);
				break;
			case GameState.NextLevel:
				UpdateNextLevel(mode);
				break;
			case GameState.Options:
				UpdateOptions(mode);
				break;
			case GameState.Help:
				UpdateHelp(mode);
				break;
			case GameState.HighScore:
				UpdateHighScore(mode);
				break;
		}	
	}
	
	public void SwitchState(int nextState)
	{
		UpdateGame(UpdateType.DTOR);
		s_iCurrentState = nextState;		
		UpdateGame(UpdateType.CTOR);
	}
	public void SwitchState1(int nextState)
	{
		//UpdateGame(UpdateType.DTOR);
		s_iCurrentState = nextState;		
		UpdateGame(UpdateType.CTOR);
	}
	public void SwitchState2(int nextState)
	{
		UpdateGame(UpdateType.DTOR);
		s_iCurrentState = nextState;		
		//UpdateGame(UpdateType.CTOR);
	}
	
	
	public void run()
	{
		//s_iCurrentState = GameState.GameloftLogo;		
		UpdateGame(UpdateType.CTOR);
		while (true)
		{
			m_lBeginTime = System.currentTimeMillis();
			UpdateGame(UpdateType.UPDATE);	
			repaint();
			serviceRepaints();			
			m_lCurrentTime = System.currentTimeMillis();
            s_frameCount++;	
            if (s_frameCount == 30)
				s_frameCount = 0;			
			if((m_lCurrentTime-m_lBeginTime)<80)
			{		
				try
				{
				Thread.sleep(80-(m_lCurrentTime-m_lBeginTime));
				} catch (Exception e) {}
			
				if (isInterrupted)
				{
				isInterrupted = false;
				}
			}
						
		}
		
	}
	
	public void paint(Graphics g)
	{
		ClearKey();
		s_g = g;
		UpdateGame(UpdateType.PAINT);
        drawSoftKey();		
	}	
	
	#include "cGame_Key.h"
	#include "cGame_Ingame.h"
	#include "cGame_Logo.h"
	#include "cGame_Loading.h"
	#include "cGame_Splash.h"
	#include "cGame_Sound.h"
	#include "cGame_MainMenu.h"
	#include "CFont.h"
	#include "cGame_About.h"
	#include "cGame_Exit.h"
	#include "cGame_Level1.h"
	#include "cGame_Level2.h"
	#include "cGame_Level3.h"
	#include "cGame_NextLevel.h"
	#include "cGame_Options.h"
	#include "cGame_IGM.h"
	#include "cGame_DrawSoftKey.h"
	#include "cGame_Help.h"
	#include "cGame_HighScore.h"
	//#include "cGame_Public.h"
	
}